package br.mil.cpp.estudo;

import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;

public class JInternalFrameDemo {//extends JFrame {

	JDesktopPane jdpDesktop;
	static int openFrameCount = 0;
 
	public JInternalFrameDemo() {
		super();
		// Make the main window positioned as 50 pixels from each edge of the
		// screen.
		int inset = 50;
		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
		setBounds(inset, inset, screenSize.width - inset * 2, screenSize.height
				- inset * 2);
		// Add a Window Exit Listener
		addWindowListener(new WindowAdapter() {

			public void windowClosing(WindowEvent e) {
				System.exit(0);
			}
		});
		// Create and Set up the GUI.
		jdpDesktop = new JDesktopPane();
		// A specialized layered pane to be used with JInternalFrames
		createFrame(); // Create first window
		setContentPane(jdpDesktop);
		setJMenuBar(createMenuBar());
		// Make dragging faster by setting drag mode to Outline
		jdpDesktop.putClientProperty("JDesktopPane.dragMode", "outline");
	}

	private void setJMenuBar(JMenuBar createMenuBar) {
		// TODO Auto-generated method stub
		
	}

	private void setContentPane(JDesktopPane jdpDesktop2) {
		// TODO Auto-generated method stub
		
	}

	private void addWindowListener(WindowAdapter windowAdapter) {
		// TODO Auto-generated method stub
		
	}

	private void setBounds(int inset, int inset2, int i, int j) {
		// TODO Auto-generated method stub
		
	}

	protected JMenuBar createMenuBar() {
		JMenuBar menuBar = new JMenuBar();
		JMenu menu = new JMenu("Frame");
		menu.setMnemonic(KeyEvent.VK_N);
		JMenuItem menuItem = new JMenuItem("New IFrame");
		menuItem.setMnemonic(KeyEvent.VK_N);
		menuItem.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent e) {
				if(openFrameCount <= 1) {
					createFrame();
					openFrameCount = 0;
				}
			}
		});
		menu.add(menuItem);
		menuBar.add(menu);
		return menuBar;
	}

	protected void createFrame() {
		MyInternalFrame frame = new MyInternalFrame();
		frame.setVisible(true);
		// Every JInternalFrame must be added to content pane using JDesktopPane
		jdpDesktop.add(frame);
		try {
			frame.setSelected(true);
		} catch (java.beans.PropertyVetoException e) {
		}
	}

	public static void main(String[] args) {
		JInternalFrameDemo frame = new JInternalFrameDemo();
		frame.setVisible(true);
	}

	private void setVisible(boolean b) {
		// TODO Auto-generated method stub
		
	}

	class MyInternalFrame extends JInternalFrame {

		static final int xPosition = 30, yPosition = 30;

		public MyInternalFrame() {
			super("IFrame #" + (++openFrameCount), true, // resizable
					true, // closable
					true, // maximizable
					true);// iconifiable
			setSize(300, 300);
			// Set the window's location.
			setLocation(xPosition * openFrameCount, yPosition * openFrameCount);
		}
	}
}